﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using System.Data.SqlClient;
using Nova_Common;

public partial class Storage_UpdatePOP : System.Web.UI.Page
{
    Nova_DAL.DbHelper access = new DbHelper();
    DbCommand cmd = null;
    DataSet ds = null;
    string strSQL = "";
    string strPID = "";
    int j = 0;
    decimal decNum = 0;
    string strWMSName = "";    //是增加库存，还是减少库存

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            strPID = Request["PID"].ToString();
        }
        catch (Exception ex)
        {

        }

        if (!IsPostBack)
        {
            getDS();
        }
    }

    protected void getDS()
    {
        strSQL = @"SELECT TBS.Mat_Num,TBM.Mat_Code,TBM.Mat_Name,TBM.Unit_Base,TBM.TM02,TBM.TM03,TBM.S01
                    FROM TBase_Storage TBS 
                    LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID = TBS.Sec_Mat_ID 
                    LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TBI.Mat_Code WHERE TBS.PID = '" + strPID + "' ";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds.Tables[0].Rows.Count > 0)
        {
            hidden.Value=txt_Mat_Num.Text = ds.Tables[0].Rows[0]["Mat_Num"].ToString();
            txtMat_Code.Text = ds.Tables[0].Rows[0]["Mat_Code"].ToString();
            txtMat_Name.Text = ds.Tables[0].Rows[0]["Mat_Name"].ToString();
            txtUnit_Base.Text = ds.Tables[0].Rows[0]["Unit_Base"].ToString();
            txtS01.Text = ds.Tables[0].Rows[0]["S01"].ToString();
        }
    }



    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        string strTrayID = "";
        string strMatCode = "";
        Int64 PosID = 0;
        strSQL = "Update TBase_Storage SET Mat_Num = '" + txt_Mat_Num.Text + "' WHERE PID = '" + strPID + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        int i = access.ExecuteNonQuery(cmd);
        if (i > 0)
        {
            strSQL = @"SELECT TBP.Pos_ID,TBS.Sec_Mat_ID,TRP.Tray_ID,TBT.Relations_ID,TRT.ContainerID,TBI.MAT_CODE
                        FROM TBase_Storage TBS 
                        LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID = TBS.ContainerID 
                        LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TRT.Relations_ID 
                        LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
                        LEFT JOIN TBase_Position TBP ON TBP.Pos_ID = TRP.Pos_ID
						LEFT JOIN dbo.TBase_Interface AS TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID WHERE TBS.PID = '" + strPID + "'";
            cmd = access.GetSqlStringCommond(strSQL);
            DataSet PDS = access.ExecuteDataSet(cmd);
            if (PDS.Tables[0].Rows.Count > 0)
            {
                strTrayID = PDS.Tables[0].Rows[0]["Tray_ID"].ToString();
                strMatCode = PDS.Tables[0].Rows[0]["MAT_CODE"].ToString();
                PosID = Convert.ToInt64(PDS.Tables[0].Rows[0]["Pos_ID"].ToString());
                string strType = "";       //70，80。  70表示增加库存，80表示减少库存
                decNum = 0;        //变化数量


                if (Convert.ToDecimal(txt_Mat_Num.Text) > Convert.ToDecimal(hidden.Value.ToString()))
                {
                    strWMSName = "WMS直接添加库存";
                    decNum = Convert.ToDecimal(Convert.ToDecimal(txt_Mat_Num.Text) - Convert.ToDecimal(hidden.Value.ToString()));
                    strType = "70";
                }
                else
                {
                    strWMSName = "WMS直接删除库存";
                    decNum = Convert.ToDecimal(Convert.ToDecimal(hidden.Value.ToString()) - Convert.ToDecimal(txt_Mat_Num.Text));
                    strType = "80";
                }
                strSQL = "INSERT INTO THistory(TaskID,TaskMode,TaskModeName,CurPos_ID,Tray_ID,TarPos_ID,Sec_Mat_ID,Mat_Num,XType,END_Sec_Mat_ID,Relations_ID,ContainerID,OID)values('0','" + strType + "','" + strWMSName + "','" + PDS.Tables[0].Rows[0]["Pos_ID"].ToString() + "','" + PDS.Tables[0].Rows[0]["Tray_ID"].ToString() + "','" + PDS.Tables[0].Rows[0]["Pos_ID"].ToString() + "','" + PDS.Tables[0].Rows[0]["Sec_Mat_ID"].ToString() + "','" + decNum + "','1','" + PDS.Tables[0].Rows[0]["Sec_Mat_ID"].ToString() + "','" + PDS.Tables[0].Rows[0]["Relations_ID"].ToString() + "','" + PDS.Tables[0].Rows[0]["ContainerID"].ToString() + "','" + Request.Cookies["vNovaUserNo"].Value.ToString() + "')";
                cmd = access.GetSqlStringCommond(strSQL);
                j = access.ExecuteNonQuery(cmd);

                this.strSQL = "select * from TBase_Storage where Mat_Num='0' and  PID = '" + this.strPID + "'";
                this.cmd = this.access.GetSqlStringCommond(this.strSQL);
                this.ds = this.access.ExecuteDataSet(this.cmd);
                if (this.ds.Tables[0].Rows.Count > 0)
                {
                    this.strSQL = "delete from TBase_Storage where PID = '" + this.strPID + "'";
                    this.cmd = this.access.GetSqlStringCommond(this.strSQL);
                    this.access.ExecuteNonQuery(this.cmd);

                    strSQL = @" INSERT INTO tbTrayMaterial  ([Pos_ID],[Tray_ID],[Mat_Code]) VALUES(@Pos_ID,@TRAY_ID,@MAT_CODE)";
                    cmd = access.GetSqlStringCommond(strSQL);
                    access.AddInParameter(cmd, "@Pos_ID", DbType.Int64, PosID);
                    access.AddInParameter(cmd, "@TRAY_ID", DbType.String, strTrayID);
                    access.AddInParameter(cmd, "@MAT_CODE", DbType.String, strMatCode);
                    access.ExecuteNonQuery(cmd);
                }

            }
        }
        if (j > 0)
        {
            ShowBox.MessageBox(this.Page, strWMSName + "操作成功！");
            this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "window.returnValue='1';top.close();", true);
        }
    }

    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "top.close();", true);
    }
}
